home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0143_JPEG Specs.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  9KB  |  261 lines

  1. {
  2. From: inof@asterix.rz.tu-clausthal.de (Oliver Fromme (TBH))
  3.  
  4. > I need to be able to get the size and colors from a JPEG/JFIF image file..
  5. > Nothing more, nothing less... Structures would do, regardless of
  6. > language (C, Asm, Pas, Basic). Anyone?
  7.  
  8. =============================================================================
  9.  
  10. This file was written by:
  11.  
  12.    Oliver Fromme
  13.    Leibnizstr. 18-61
  14.    38678 Clausthal
  15.    GERMANY
  16.  
  17. Email:  fromme@rz.tu-clausthal.de
  18.  
  19.  
  20. General Notes:
  21. ~~~~~~~~~~~~~~
  22.  
  23.   - All product names mentioned in this file are trademarks or registered
  24.     trademarks of their respective owners.
  25.   - This file contains information about the JPEG/JFIF format
  26.   - This is NO official documentation, for private purposes only.
  27.     There may be bugs and errors in this file.  Use the information
  28.     on your own risk.
  29.   - This file does NOT explain the principles of JPEG coding/decoding,
  30.     i.e. DCT/IDCT, quantization, entropy codec.  I assume that the reader
  31.     is familiar with these algorithms.
  32.   - For further information please refer to the JPEG ISO standard.
  33.   - The JPEG/JFIF format uses Motorola format for words, NOT Intel format,
  34.     i.e. high byte first, low byte last.
  35.  
  36.  
  37. JPEG/JFIF file format:
  38. ~~~~~~~~~~~~~~~~~~~~~~
  39.  
  40.   - header (2 bytes):  $ff, $d8 (SOI) (these two identify a JPEG/JFIF file)
  41.   - for JFIF files, an APP0 segment is immediately following the SOI marker,
  42.     see below
  43.   - any number of "segments" (similar to IFF chunks), see below
  44.   - trailer (2 bytes): $ff, $d9 (EOI)
  45.  
  46.  
  47. Segment format:
  48. ~~~~~~~~~~~~~~~
  49.  
  50.   - header (4 bytes):
  51.        $ff     identifies segment
  52.         n      type of segment (one byte)
  53.        sh, sl  size of the segment, including these two bytes, but not
  54.                including the $ff and the type byte. Note, not intel order:
  55.                high byte first, low byte last!
  56.   - contents of the segment, max. 65533 bytes.
  57.  
  58.  Notes:
  59.   - There are parameterless segments (denoted with a '*' below) that DON'T
  60.     have a size specification (and no contents), just $ff and the type byte.
  61.   - Any number of $ff bytes between segments is legal and must be skipped.
  62.  
  63.  
  64. Segment types:
  65. ~~~~~~~~~~~~~~
  66.  
  67.    *TEM   = $01   usually causes a decoding error, may be ignored
  68.  
  69.     SOF0  = $c0   Start Of Frame (baseline JPEG), for details see below
  70.     SOF1  = $c1   dito
  71.     SOF2  = $c2   usually unsupported
  72.     SOF3  = $c3   usually unsupported
  73.  
  74.     SOF5  = $c5   usually unsupported
  75.     SOF6  = $c6   usually unsupported
  76.     SOF7  = $c7   usually unsupported
  77.  
  78.     SOF9  = $c9   for arithmetic coding, usually unsupported
  79.     SOF10 = $ca   usually unsupported
  80.     SOF11 = $cb   usually unsupported
  81.  
  82.     SOF13 = $cd   usually unsupported
  83.     SOF14 = $ce   usually unsupported
  84.     SOF15 = $cf   usually unsupported
  85.  
  86.     DHT   = $c4   Define Huffman Table, for details see below
  87.     JPG   = $c8   undefined/reserved (causes decoding error)
  88.     DAC   = $cc   Define Arithmetic Table, usually unsupported
  89.  
  90.    *RST0  = $d0   RSTn are used for resync, may be ignored
  91.    *RST1  = $d1
  92.    *RST2  = $d2
  93.    *RST3  = $d3
  94.    *RST4  = $d4
  95.    *RST5  = $d5
  96.    *RST6  = $d6
  97.    *RST7  = $d7
  98.  
  99.     SOI   = $d8   Start Of Image
  100.     EOI   = $d9   End Of Image
  101.     SOS   = $da   Start Of Scan, for details see below
  102.     DQT   = $db   Define Quantization Table, for details see below
  103.     DNL   = $dc   usually unsupported, ignore
  104.     DRI   = $dd   Define Restart Interval, for details see below
  105.     DHP   = $de   ignore (skip)
  106.     EXP   = $df   ignore (skip)
  107.  
  108.     APP0  = $e0   JFIF APP0 segment marker, for details see below
  109.     APP15 = $ef   ignore
  110.  
  111.     JPG0  = $f0   ignore (skip)
  112.     JPG13 = $fd   ignore (skip)
  113.     COM   = $fe   Comment, may be ignored
  114.  
  115.  All other segment types are reserved and should be ignored (skipped).
  116.  
  117.  
  118. SOF0: Start Of Frame 0:
  119. ~~~~~~~~~~~~~~~~~~~~~~~
  120.  
  121.   - $ff, $c0 (SOF0)
  122.   - length (high byte, low byte), 8+components*3
  123.   - data precision (1 byte) in bits/sample, usually 8 (12 and 16 not
  124.     supported by most software)
  125.   - image height (2 bytes, Hi-Lo), must be >0 if DNL not supported
  126.   - image width (2 bytes, Hi-Lo), must be >0 if DNL not supported
  127.   - number of components (1 byte), usually 1 = grey scaled, 3 = color YCbCr
  128.     or YIQ, 4 = color CMYK)
  129.   - for each component: 3 bytes
  130.      - component id (1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q)
  131.      - sampling factors (bit 0-3 vert., 4-7 hor.)
  132.      - quantization table number
  133.  
  134.  Remarks:
  135.   - JFIF uses either 1 component (Y, greyscaled) or 3 components (YCbCr,
  136.     sometimes called YUV, colour).
  137.  
  138.  
  139. APP0: JFIF segment marker:
  140. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  141.  
  142.   - $ff, $e0 (APP0)
  143.   - length (high byte, low byte), must be >= 16
  144.   - 'JFIF'#0 ($4a, $46, $49, $46, $00), identifies JFIF
  145.   - major revision number, should be 1 (otherwise error)
  146.   - minor revision number, should be 0..2 (otherwise try to decode anyway)
  147.   - units for x/y densities:
  148.      0 = no units, x/y-density specify the aspect ratio instead
  149.      1 = x/y-density are dots/inch
  150.      2 = x/y-density are dots/cm
  151.   - x-density (high byte, low byte), should be <> 0
  152.   - y-density (high byte, low byte), should be <> 0
  153.   - thumbnail width (1 byte)
  154.   - thumbnail height (1 byte)
  155.   - n bytes for thumbnail (RGB 24 bit), n = width*height*3
  156.  
  157.  Remarks:
  158.   - If there's no 'JFIF'#0, or the length is < 16, then it is probably not
  159.     a JFIF segment and should be ignored.
  160.   - Normally units=0, x-dens=1, y-dens=1, meaning that the aspect ratio is
  161.     1:1 (evenly scaled).
  162.   - JFIF files including thumbnails are very rare, the thumbnail can usually
  163.     be ignored.  If there's no thumbnail, then width=0 and height=0.
  164.   - If the length doesn't match the thumbnail size, a warning may be
  165.     printed, then continue decoding.
  166.  
  167.  
  168. DRI: Define Restart Interval:
  169. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170.  
  171.   - $ff, $dd (DRI)
  172.   - length (high byte, low byte), must be = 4
  173.   - restart interval (high byte, low byte) in units of MCU blocks,
  174.     meaning that every n MCU blocks a RSTn marker can be found.
  175.     The first marker will be RST0, then RST1 etc, after RST7
  176.     repeating from RST0.
  177.  
  178.  
  179. DQT: Define Quantization Table:
  180. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  181.  
  182.   - $ff, $db (DQT)
  183.   - length (high byte, low byte)
  184.   - QT information (1 byte):
  185.      bit 0..3: number of QT (0..3, otherwise error)
  186.      bit 4..7: precision of QT, 0 = 8 bit, otherwise 16 bit
  187.   - n bytes QT, n = 64*(precision+1)
  188.  
  189.  Remarks:
  190.   - A single DQT segment may contain multiple QTs, each with its own
  191.     information byte.
  192.   - For precision=1 (16 bit), the order is high-low for each of the 64 words.
  193.  
  194.  
  195. DAC: Define Arithmetic Table:
  196. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  197.  
  198.  Current software does not support arithmetic coding for legal reasons.
  199.  JPEG files using arithmetic coding can not be processed.
  200.  
  201.  
  202. DHT: Define Huffman Table:
  203. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  204.  
  205.   - $ff, $c4 (DHT)
  206.   - length (high byte, low byte)
  207.   - HT information (1 byte):
  208.      bit 0..3: number of HT (0..3, otherwise error)
  209.      bit 4   : type of HT, 0 = DC table, 1 = AC table
  210.      bit 5..7: not used, must be 0
  211.   - 16 bytes: number of symbols with codes of length 1..16, the sum of these
  212.     bytes is the total number of codes, which must be <= 256
  213.   - n bytes: table containing the symbols in order of increasing code length
  214.     (n = total number of codes)
  215.  
  216.  Remarks:
  217.   - A single DHT segment may contain multiple HTs, each with its own
  218.     information byte.
  219.  
  220.  
  221. SOS: Start Of Scan:
  222. ~~~~~~~~~~~~~~~~~~~
  223.  
  224.   - $ff, $da (SOS)
  225.   - length (high byte, low byte), must be 6+2*(number of components in scan)
  226.   - number of components in scan (1 byte), must be >= 1 and <=4 (otherwise
  227.     error), usually 1 or 3
  228.   - for each component: 2 bytes
  229.      - component id (1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q), see SOF0
  230.      - Huffman table to use:
  231.         - bit 0..3: AC table (0..3)
  232.         - bit 4..7: DC table (0..3)
  233.   - 3 bytes to be ignored (???)
  234.  
  235.  Remarks:
  236.   - The image data (scans) is immediately following the SOS segment.
  237.  
  238.  
  239. End of part 1.
  240. =============================================================================
  241.  
  242. I've written a JPEG Decoding Unit for Borland/Turbo Pascal 7.0.  It's very
  243. fast, since it uses Assembly routines for the critical algorithms.
  244. Check out the program QPEG which is a shareware image viewer (JPEG, GIF,
  245. Targa, PCX, BMP) -- it uses that unit.  The QPEG package also contains a
  246. more detailed description of the JPEG unit and an order form for it
  247. (including source code).  I'm also writing a JPEG decoding DLL which will
  248. be available soon.
  249.  
  250. If you have access to the Internet, then you can get QPEG via FTP from one
  251. of these sites:
  252.    ftp.tu-clausthal.de  /pub/msdos/graphics (primary site, Germany)
  253.    ftp.rahul.net        /pub/bryanw/qpeg
  254.    wuarchive.wustl.edu  /pub/msdos_uploads/graphics
  255. If you don't have Internet access, just send 5 $US or 5 DM (cash) to me
  256. (to cover my expenses), and you'll get the shareware version of QPEG.
  257. You must have at least a 386 processor and a VGA graphics card.
  258.  
  259. See my address (normal mail and electronic mail) at the top of this file.
  260.  
  261. }